package com.example.rxpermissiondemo;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class RxPermissionActivity extends Activity {
    private static final String TAG = "RxPermissionActivity";
    RxPermissions rxPermissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);

        ButterKnife.bind(this);

        rxPermissions = new RxPermissions(this);
        rxPermissions.setLogging(true);

    }

    @OnClick(R.id.btn_request)
    public void onRequest() {
        rxPermissions
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            // 有权限了
                            Log.e(TAG, "accept: 有相机权限！");
                        } else {
                            // 没有权限
                            Log.e(TAG, "accept: 没有相机权限！");
                        }
                    }
                });
    }

    @OnClick(R.id.btn_requesteach)
    public void onRequestEach() {
        rxPermissions
                .requestEach(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 授予了权限
                            Log.e(TAG, "accept: 授予了" + permission.name + "权限");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 拒绝了权限，但是没有勾选不再询问
                            Log.e(TAG, "accept: 拒绝了" + permission.name + "权限，但是没有勾选不再询问");
                        } else {
                            // 拒绝了权限，且勾选了不再询问，跳转"设置"中手动授予权限
                            Log.e(TAG, "accept: 拒绝了" + permission.name + "权限，且勾选不再询问");
                        }
                    }
                });
    }

    @OnClick(R.id.btn_requesteach_combined)
    public void onRequestEachCombined() {
        rxPermissions
                .requestEachCombined(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // 所有申请的权限都被授予了
                            Log.e(TAG, "accept: 授予了" + permission.name + "权限");
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 申请的权限，至少有一个被拒绝了，且没有勾选不再询问
                            Log.e(TAG, "accept: 拒绝了" + permission.name + "权限，但是没有勾选不再询问");
                        } else {
                            // 申请的权限，至少有一个被拒绝，且勾选了不再询问
                            Log.e(TAG, "accept: 拒绝了" + permission.name + "权限，且勾选不再询问");
                        }
                    }
                });
    }

}

